import {basePath, authorization} from "./client.js";

let path = basePath + '/doctor'

// 获取所有doctor
export const getAllDoctor = async () => {
    const res = await fetch(path + '/findAll', {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

// 获取所有doctor(在某个时间范围内)
export const getAllDoctorInDate = async ({start,end}) => {
    const res = await fetch(path + `/find/${start}/${end}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

// 分页 获取某页
export const getAllDoctorPage = async ({page, size}) => {
    const res = await fetch(path + `/page?page=${page}&size=${size}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

// 获取总数
export const getDoctorCount = async () => {
    const res = await fetch(path + `/count`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

// 根据id删除doctor
export const delDoctor = async (id) => {
    const res = await fetch(path + `/delete/${id}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'DELETE',
        mode: 'cors'
    })
    return res.json()
}

// 根据id查找doctor
export const findDoctorById = async (id) => {
    const res = await fetch(path + `/find/${id}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

// 根据id修改
export const updateDoctor = async (data) => {
    const res = await fetch(path + `/dto/save`,
        {
            body: JSON.stringify(data),
            headers: {
                'content-type': 'application/json',
                'authorization': authorization
            },
            method: 'PUT',
            mode: 'cors'
        })
    return res.json()
}

// 新增
export const saveDoctor = async (data) => {
    const res = await fetch(path + `/dto/save`,
        {
            body: JSON.stringify(data),
            headers: {
                'content-type': 'application/json',
                'authorization': authorization
            },
            method: 'POST',
            mode: 'cors'
        })
    return res.json()
}

// 根据name查找(like)
export const findByName = async (name) => {
    const res = await fetch(path + `/find/name/${name}`,
        {
            headers: {
                'content-type': 'application/json',
                'authorization': authorization
            },
            method: 'GET',
            mode: 'cors'
        })
    return res.json()
}